php validate integer [updated]

Posted by George Garchagudashvili on Stack Overflow See other posts from Stack Overflow or by George Garchagudashvili
Published on 2014-08-24T21:41:45Z Indexed on 2014/08/24 22:20 UTC
Read the original article Hit count: 105

Filed under:
|

Read B

A:

I'll give quick example:

$a = "\n  \t 34 3"; // string(9)

$aint = intval($a); // int(34)

var_dump($a == $aint);

result:

bool(true)

call me noob but can you tell me why/how does these variables do pass equalization test?

What I want to achieve is to check if '1989' equals 1989 would be true, but not any other case. ex: '1989 ' should not pass the test. Also I don't want to use regex.

B:

I need to validate if variable is integer, I've tried all the available built-in functions or helpful tips, but the only best solution would be regex which I don't want to use this time.

also filter_var is not best because it also filters data, but I want to only validate it.

123
-123
'123'
'-123'

these inputs to be only true, false otherwise

I've tried many different options:

ctype_digit("-123"); // false - doesn't work
is_int('123'); // false 
filter_var('   123 ', FILTER_VALIDATE_INT) !== false; // true - doesn't work

© Stack Overflow or respective owner

Related posts about php

Related posts about equality